package cn.xin.search.service.impl;

import cn.xin.base.jwt.JwtUtils;
import cn.xin.base.util.*;
import cn.xin.org.domain.Shop;
import cn.xin.org.mapper.ShopMapper;
import cn.xin.search.controller.SearchMasterMsgAuditLogController;
import cn.xin.search.domain.SearchMasterMsg;
import cn.xin.search.domain.SearchMasterMsgAuditLog;
import cn.xin.search.dto.SearchMasterMsgDto;
import cn.xin.search.mapper.SearchMasterMsgAuditLogMapper;
import cn.xin.search.mapper.SearchMasterMsgMapper;
import cn.xin.search.service.ISearchMasterMsgService;
import cn.xin.base.service.impl.BaseServiceImpl;
import cn.xin.user.domain.Logininfo;
import cn.xin.user.domain.User;
import cn.xin.user.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xin
 * @since 2023-01-10
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

//    @Value("${jwt.pub}")
//    private String jwtPub;

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SearchMasterMsgAuditLogMapper searchMasterMsgAuditLogMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Override
    public AjaxResult publish(SearchMasterMsgDto dto) {
        //校验参数
        //保存数据
        SearchMasterMsg masterMsg = new SearchMasterMsg();
        BeanUtils.copyProperties(dto,masterMsg);
        //添加状态
        masterMsg.setState(0);

        //获取登录信息
        Logininfo logininfo = LoginContext.getUserInfo().getLogininfo();
        //通过logininfo获取userid
        User user = userMapper.findByLoginInfoId(logininfo.getId());

        masterMsg.setUserId(user.getId());
        //添加数据
        super.add(masterMsg);

        //百度AI审核
        if(!BaiduAuditUtils.TextCensor(dto.getName())){
            //审核不通过
            //添加日志
            SearchMasterMsgAuditLog msgAuditLog = new SearchMasterMsgAuditLog();
            msgAuditLog.setMsgId(masterMsg.getId());
            msgAuditLog.setState(0);
            msgAuditLog.setAuditId(-1L);
            msgAuditLog.setAuditTime(new Date());
            msgAuditLog.setNote("审核未通过" + dto.getName());
            searchMasterMsgAuditLogMapper.add(msgAuditLog);

            //添加后修改State
            masterMsg.setState(2);

        }else {
            //审核通过
            //获取用户的地址去找附近的店铺
            Point point = DistanceUtil.getPoint(dto.getAddress());
            //获取所有店铺
            List<Shop> shopList = shopMapper.findAll();
            //获取离用户近的店铺
            Shop nearestShop = DistanceUtil.getNearestShop(point, shopList);

            if (nearestShop != null){
                //如何店铺不为空，那就把当前店铺的id给到
                masterMsg.setShopId(nearestShop.getId());
            }

            //更新状态
            masterMsg.setState(1);

        }

        searchMasterMsgMapper.update(masterMsg);
        return AjaxResult.me().success();
    }
}
